//
//  HRZGoodsGridTableCell.m
//  EMarket
//
//  Created by Tyrone on 2018/5/24.
//
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZGoodsGridTableCell.h"

// Controllers

// Models
#import "HRZGoodsTypeModel.h"
#import "HRZGoodsModel.h"
// Views
#import "HRZShowGoodsSimpleInfoCell.h"
#import "HRZGoodsTypeHeadView.h"
// Vendors

// Categories
#import "HRZLayoutBase.h"
// Others

#define GoodsTypeCollectionHeaderHeight     30
#define CollectionItemWidth                 (kScreenWidth - 10)/2

@interface HRZGoodsGridTableCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    CGSize itemSize;
    HRZLayoutBase *_layout;
}

@property (weak, nonatomic) IBOutlet UICollectionView *mCollectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionLayoutHeight;

@end

static NSString * const HRZGoodsTypeHeadViewID          = @"HRZGoodsTypeHeadView";
static NSString * const HRZShowGoodsSimpleInfoCellID    = @"HRZShowGoodsSimpleInfoCell";

@implementation HRZGoodsGridTableCell

- (void)updateConstraints {
    [super updateConstraints];
    CGFloat totalHeight = 0;
    totalHeight += _goodsTypeArray.count * GoodsTypeCollectionHeaderHeight;
    for (HRZGoodsTypeModel *goodsType in _goodsTypeArray) {
        
        int row = (int)goodsType.goodsArray.count / 2;
        int mor = (int)goodsType.goodsArray.count % 2;
        totalHeight += (itemSize.height + 10) * (row + mor);
    }
    totalHeight += 20;
    
    self.collectionLayoutHeight.constant = totalHeight;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    itemSize = CGSizeMake(CollectionItemWidth, CollectionItemWidth + 30);
    _layout = [[HRZLayoutBase alloc] init];
   
    self.mCollectionView.backgroundColor = HRZBGColor;
    self.mCollectionView.delegate = self;
    self.mCollectionView.dataSource = self;
    [self.mCollectionView setCollectionViewLayout:_layout];
    
    //注册
    /** cell */
    [_mCollectionView registerClass:[HRZShowGoodsSimpleInfoCell class] forCellWithReuseIdentifier:HRZShowGoodsSimpleInfoCellID];
    
    /** header */
    [_mCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HRZGoodsTypeHeadView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HRZGoodsTypeHeadViewID];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - ===============  Set  ===============
- (void)setGoodsTypeArray:(NSMutableArray<HRZGoodsTypeModel *> *)goodsTypeArray {
    _goodsTypeArray = goodsTypeArray;
    [_mCollectionView reloadData];
}

#pragma mark - ===============  UICollectionViewDateSource  ===============
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _goodsTypeArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    HRZGoodsTypeModel *typeModel = _goodsTypeArray[section];
    return typeModel.goodsArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HRZShowGoodsSimpleInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HRZShowGoodsSimpleInfoCellID forIndexPath:indexPath];
    HRZGoodsTypeModel *typeModel = _goodsTypeArray[indexPath.section];
    cell.model = typeModel.goodsArray[indexPath.row];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView * reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        HRZGoodsTypeHeadView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HRZGoodsTypeHeadViewID forIndexPath:indexPath];
        header.typeName = @"新品";
        reusableView = header;
    }
    return reusableView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return itemSize;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(kScreenWidth, GoodsTypeCollectionHeaderHeight);
}
#pragma mark - CollectionHeaderView

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(delegateTableCell:didSelectedItemAtIndexPath:)]) {
        [self.delegate delegateTableCell:self didSelectedItemAtIndexPath:indexPath];
    }
}

@end
